//
//  PhotoAnimator.swift
//  PhotoBrowser
//
//  Created by 游强辉 on 16/4/28.
//  Copyright © 2016年 youqianghui. All rights reserved.
//

import UIKit

// MARK:定义协议
protocol PresentDelegate : class {
    func homeRect(indexPath : NSIndexPath) -> CGRect
    func photoBrowserRect(indexPath : NSIndexPath) -> CGRect
    func imageView(IndexPath : NSIndexPath) -> UIImageView
}

protocol DismissDelegate : class {
    func currentIndexPath() -> NSIndexPath
    func imageView() -> UIImageView
}

class PhotoAnimator: NSObject {
    private var isPresent : Bool = false
    
    weak var presentDelegate : PresentDelegate?
    weak var dismissDelegate : DismissDelegate?
    var indexPath : NSIndexPath?
}

//MARK:-专场动画的代理
extension PhotoAnimator : UIViewControllerTransitioningDelegate {
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        return self
    }
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        return self
    }
}

extension PhotoAnimator : UIViewControllerAnimatedTransitioning {
    
    //1.返回动画的执行时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return 1.0
        
    }
    
    // 2.可以获取转场的上下文:可以通过上下文获取到执行动画的View
    // UITransitionContextFromViewKey : 取出是消失的View,是在做消失动画时会使用
    // UITransitionContextToViewKey : 取出是弹出的View,是在做弹出动画时会使用
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        isPresent ? animateForPresentView(transitionContext) : animateForDismissView(transitionContext)
    }
    
    func animateForPresentView(transitionContext: UIViewControllerContextTransitioning) {
        //1.取出弹出的View(控制器的View)
        let presentView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        //2.将弹出的View添加到自己的containerView中
        transitionContext.containerView()?.addSubview(presentView)
        
        //3.执行动画
        //3.1获取执行动画的ImageView
        guard let presentDelegate = presentDelegate else {
            return
        }
        
        guard let indexPath = indexPath else {
            return
        }
        
        let imageView = presentDelegate.imageView(indexPath)
        
        //3.2将imageView添加到containerView中
        transitionContext.containerView()?.addSubview(imageView)
        
        //3.3设置imageView的初始位置
        imageView.frame = presentDelegate.homeRect(indexPath)
        
        //3.4 执行动画
        presentView.alpha = 0.0
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
            //3.5设置ImageView的结束位置
            imageView.frame = presentDelegate.photoBrowserRect(indexPath)
            }) { (_) -> Void in
                presentView.alpha = 1.0
                
                imageView.removeFromSuperview()
                //4.关闭上下文
                transitionContext.completeTransition(true)
        }
    }
    
    func animateForDismissView(transitionContext: UIViewControllerContextTransitioning) {
        
        //1.取出消失的View
        let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        dismissView.removeFromSuperview()
        
        //2.执行动画
        //2.1取出执行动画的ImageView
        guard let dismissDelegate = dismissDelegate else {
            return
        }
        guard let presentDelegate = presentDelegate else {
            return
        }
        let imageView = dismissDelegate.imageView()
        transitionContext.containerView()?.addSubview(imageView)
        
        // 2.2.获取当前正在显示的indexPath
        let indexPath = dismissDelegate.currentIndexPath()
        
        //2.3设置起始位置
        imageView.frame = presentDelegate.photoBrowserRect(indexPath)
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            //2.4结束位置
            imageView.frame = presentDelegate.homeRect(indexPath)
            
            }) { (_) -> Void in
                imageView.removeFromSuperview()
                transitionContext.completeTransition(true)
        }
    }
    
    
}